package com.yonyou.pmclouds.billview.rmiimpl.buzi;

import com.yonyou.pmclouds.basecom.consts.BillTypeConst;
import com.yonyou.pmclouds.basecom.entity.PageObject;
import com.yonyou.pmclouds.basecom.entity.SuperVO;
import com.yonyou.pmclouds.basecom.exception.BusinessException;
import com.yonyou.pmclouds.basecom.util.CommonFieldConst;
import com.yonyou.pmclouds.billview.entity.BillViewQueryVO;
import com.yonyou.pmclouds.billview.rmitf.IBillViewBuziService;
import com.yonyou.pmclouds.billview.util.BillViewUtil;
import com.yonyou.pmclouds.constructionlog.entity.ConstructionLogVO;
import com.yonyou.pmclouds.constructionlog.rmiitf.ConstructionLogQuery;
import com.yonyou.pmclouds.inspectitem.entity.InspectItemBillStatusConst;
import com.yonyou.pmclouds.inspectitem.entity.InspectItemVO;
import com.yonyou.pmclouds.inspectitem.rmiitf.InspectItemService;
import com.yonyou.pmclouds.team.entity.TeamMemberVO;
import com.yonyou.pmclouds.team.rmiitf.TeamMemberQuery;
import org.apache.commons.lang3.ArrayUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.math.BigDecimal;
import java.text.DecimalFormat;
import java.util.Map;

@Component
public class ConstructionLogBuziServiceImpl implements IBillViewBuziService {

    @Autowired
    private ConstructionLogQuery constructionLogQuery;
    @Autowired
    private InspectItemService inspectItemService;
    @Autowired
    private TeamMemberQuery teamMemberQuery;

    @Override
    public String getBillType() {
        return BillTypeConst.CONSTRUCTIONLOG_BILLTYPE;
    }

    @Override
    public PageObject<Object> queryBillList(BillViewQueryVO billViewQueryVO) throws BusinessException {

        PageObject<Object> constructionLogVOs = constructionLogQuery.queryByCondition(billViewQueryVO.getPkProject(),billViewQueryVO.getCreators(),
                billViewQueryVO.getStartTime(),billViewQueryVO.getEndTime(),billViewQueryVO.getBillStatus(),
                billViewQueryVO.getPageStart(),billViewQueryVO.getPageSize());

        BillViewUtil.setBillStatus(constructionLogVOs.getObjects());
        // 设置施工单位
        setPkConstructUnit(constructionLogVOs);

        return constructionLogVOs;
    }

    /**
     * 设置施工单位
     *
     * @param objects
     */
    private void setPkConstructUnit(PageObject<Object> objects) throws BusinessException {
        if (objects == null || ArrayUtils.isEmpty(objects.getObjects()))
            return;

        ConstructionLogVO[] logVOS = (ConstructionLogVO[]) objects.getObjects();
        TeamMemberVO[] teamMemberVOs;
        for (ConstructionLogVO vo : logVOS) {
            // 由于每次查询不超过10条，为了简化逻辑，使用循环查询
            teamMemberVOs = teamMemberQuery.queryByProAndPkUsers(vo.getPk_project(), new String[]{vo.getCreator()});
            if (ArrayUtils.isNotEmpty(teamMemberVOs))
                vo.setPk_constructionunit(teamMemberVOs[0].getPkEnterprise());
        }
    }

    @Override
    public SuperVO queryBillDetail(String pkBill) throws BusinessException {

        ConstructionLogVO constructionLog = constructionLogQuery.queryOne(pkBill);

        TeamMemberVO[] teamMemberVOs =
                teamMemberQuery.queryByProAndPkUsers(constructionLog.getPk_project(),new String[]{constructionLog.getCreator()});

        if(ArrayUtils.isNotEmpty(teamMemberVOs)){
            String pkEnter = teamMemberVOs[0].getPkEnterprise();
            constructionLog.setPk_constructionunit(pkEnter);
        }

        BillViewUtil.setBillStatus(new ConstructionLogVO[]{constructionLog});
        BillViewUtil.setFormatDate(new ConstructionLogVO[]{constructionLog},new String[]{CommonFieldConst.CREATIONTIME},new String[]{"strCreationtime"});
        // 获取合格率
        Map<String, BigDecimal> doubleMap = inspectItemService.queryPassRateOfConstructLog(new String[]{constructionLog.getPk_constructionlog()});
        if (doubleMap.get(constructionLog.getPk_constructionlog()) != null) {
            constructionLog.setQualification_rate(doubleMap.get(constructionLog.getPk_constructionlog()) + "%");
        }

        InspectItemVO[] inspectitems = constructionLog.getInspectitems();
        String[] inspectItemPks = new String[inspectitems.length];
        for (int i = 0; i < inspectitems.length; i++) {
            inspectItemPks[i] = inspectitems[i].getPk_inspectitem();
        }
        InspectItemVO[] inspectItemVOS = inspectItemService.queryInspectItemsInfoByPks(inspectItemPks);
        for (int i = 0; i < inspectItemVOS.length; i++) {
            // 将检查项更换成数据明细的检查项
            inspectItemVOS[i].setPk_constructionlog(inspectitems[i].getPk_bill());
        }

        if (inspectitems != null) {
            for (InspectItemVO inspectItemVO : inspectItemVOS) {
                // 为数据显示处理成字符串
                if (inspectItemVO.getDatas() != null && inspectItemVO.getDatas().length > 0) {
                    StringBuilder strDatas = new StringBuilder();
                    DecimalFormat df = new DecimalFormat( "0.00");
                    for (double data : inspectItemVO.getDatas()) {
                        strDatas.append(  df.format(data) ).append(";");
                    }
                    inspectItemVO.setItemDatas(strDatas.toString());
                }
                // 质量状态
                switch (inspectItemVO.getBillstatus()) {
                    case InspectItemBillStatusConst.BILLSTATUS_PASS:
                        inspectItemVO.setItemQuality(InspectItemBillStatusConst.BILLSTATUS_PASS_MSG);
                        break;
                    case InspectItemBillStatusConst.BILLSTATUS_NO_PASS:
                        inspectItemVO.setItemQuality(InspectItemBillStatusConst.BILLSTATUS_NO_PASS_MSG);
                        break;
                    case InspectItemBillStatusConst.BILLSTATUS_NOT_FIT:
                        inspectItemVO.setItemQuality(InspectItemBillStatusConst.BILLSTATUS_NOT_FIT_MSG);
                        break;
                    default:
                        break;
                }
            }
        }
        constructionLog.setInspectitems(inspectItemVOS);
        return constructionLog;
    }
}
